package com.xxxy.dongyexuan.action;

import com.xxxy.dongyexuan.mapper.ICardMapper;
import com.xxxy.dongyexuan.mapper.IFlowMapper;
import com.xxxy.dongyexuan.po.Card;
import com.xxxy.dongyexuan.po.Flow;
import com.xxxy.dongyexuan.po.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class CardAction {

    @Autowired
    private ICardMapper mapper;


    //分页查询
    @GetMapping("/card/{pagenow}/{pagesize}")
    public PageBean findByPage(@PathVariable("pagenow") int pagenow, @PathVariable("pagesize") int pagesize){
        int from =(pagenow-1)*pagesize;//开始条数
        int total = this.mapper.getcount();//总条数
        int pages =(total-1)/pagesize+1;//计算总页数
        List data = this.mapper.findByPage(from,pagesize);
        PageBean pageBean = new PageBean();
        pageBean.setPages(pages);
        pageBean.setPagenow(pagenow);
        pageBean.setPagesize(pagesize);
        pageBean.setTotal(total);
        pageBean.setData(data);
        return pageBean;
    }



    //查询所有
    @GetMapping("/card")
    public List<Card> findAll(){
        List<Card> list = this.mapper.findAll();
        return list;
    }

    //根据员工号查询
    @GetMapping("/card/{cid}")
    public Card findById(@PathVariable("cid") int cid){
        Card card = this.mapper.findById(cid);
        return card;
    }

    //增加
    @PostMapping("/card")
    public int save(Card card){
        int result = this.mapper.save(card);
        return result;
    }

    //修改
    @PutMapping("/card")
    public int update(Card card){
        int result = this.mapper.merge(card);
        return result;
    }

    //删除
    @DeleteMapping("/card/{cid}")
    public int delete(@PathVariable("cid") int cid){
        int result = this.mapper.delete(cid);
        return result;
    }
}